package com.wolfking.jeesite.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wolfking.jeesite.config.SsoConfig;
import com.wolfking.jeesite.entity.Dict;
import com.wolfking.jeesite.entity.PageInfo;
import com.wolfking.jeesite.service.DictService;
import com.wolfking.jeesite.sign.Sign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.Objects;

/**
 * 字典的controller
 * <p>
 * 字典的增删查改
 *
 * @author wolfking@赵伟伟
 * @mail zww199009@163.com
 * @创作日期 2017年4月26日下午1:09:59
 * @版权 归wolfking所有
 */
@Slf4j
@Controller
@RequestMapping("/sys/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    private SsoConfig ssoConfig;

    /**
     * 字典的列表页，包括模糊查询
     * <p>
     *
     * @param model
     * @param dict
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Sign(code = "sys:dict:view")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public String list(Model model, Dict dict, @RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "20") int pageSize) {
        QueryWrapper<Dict> dictQueryWrapper = dictService.queryWrapperVague(dict);
        dictQueryWrapper.orderByDesc("update_time");
        PageInfo<Dict> page = dictService.pageQuery(dictQueryWrapper, pageNum, pageSize);
        List<String> allType = dictService.getAllDictType();
        model.addAttribute("page", page);
        model.addAttribute("allType", allType);
        model.addAttribute("dict", dict);
        return "sys/dict/list";
    }

    /**
     * 字典的编辑页面
     *
     * @param model
     * @param dict
     * @return
     */
    @Sign(code = "sys:dict:view")
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, Dict dict) {
        if (Objects.nonNull(dict.getId()))
            dict = dictService.getById(dict.getId());
        model.addAttribute("dict", dict);
        return "sys/dict/edit";
    }

    /**
     * 字典的修改
     *
     * @param dict
     * @param redirectAttributes
     * @return
     */
    @Sign(code = "sys:dict:edit", eventName = "字典添加修改")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String edits(Dict dict, RedirectAttributes redirectAttributes) {
        dict.setValue(dict.getValue().trim());
        dictService.saveOrUpdate(dict);
        if (Objects.nonNull(dict.getId())) {
            redirectAttributes.addFlashAttribute("message", "修改字典成功");
        } else {
            redirectAttributes.addFlashAttribute("message", "保存字典成功");
        }
        return ssoConfig.getRedirectWithPath("/sys/dict/list?type=" + dict.getType());
    }


    /**
     * 字典的复制编辑页面
     *
     * @param model
     * @param dict
     * @return
     */
    @Sign(code = "sys:dict:view")
    @RequestMapping(value = "/copyEdit", method = RequestMethod.GET)
    public String copyEdit(Model model, Dict dict) {
        if (Objects.nonNull(dict.getId())) {
            dict = dictService.getById(dict.getId());
            if (Objects.nonNull(dict)) {
                dict.setId(null);
                dict.setLabel(dict.getLabel() + "-2");
                dict.setValue(dict.getValue() + "-2");
                dict.setCreateTime(null);
                dict.setUpdateTime(null);
                dict.setVersion(0);
            } else {
                dict = new Dict();
            }
        }
        model.addAttribute("dict", dict);
        return "sys/dict/edit";
    }


    /**
     * 删除字典值
     *
     * @param id
     * @return
     */
    @Sign(code = "sys:dict:edit", eventName = "字典删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Integer id) {
        Dict dict = dictService.getById(id);
        if (Objects.nonNull(id)) {
            dictService.removeById(id);
        }
        if (Objects.isNull(dict)) {
            return ssoConfig.getRedirectWithPath("/sys/dict/list");
        } else {
            return ssoConfig.getRedirectWithPath("/sys/dict/list?type=" + dict.getType());
        }
    }

}
